package Frames;

import Controllers.SignUp_Controller;
import Controllers.SignUp_Validator;
import DAO.SignUp_Dao;
import javax.swing.JOptionPane;

public class SignUp_Frame extends javax.swing.JFrame {

    public SignUp_Frame() {
        initComponents();
        jPanel1.setName("jPanel1");
        jUsername.setName("jUsername");
        jLastName.setName("jLastName");
        jPhone.setName("jPhone");
        jEmail.setName("jEmail");
        jName.setName("jName");
        jPassword.setName("jPassword");
        jRepass.setName("jRepass");
        messageLabel.setVisible(false);
    }
//    public void createSignUp(PrintWriter signupOUTPUT) {
//        String signupINFO = "Name: " + jName.getText() + "  " + "LastName: " + jLastName.getText() + "  " + "Phone: " + jPhone.getText() + "  " + "Email: " + jEmail.getText() + "  " + "Username: " + jUsername.getText() + "  " + "Password: " + jPassword.getText() + "  " + "Repass: " + jRepass.getText(); //Contains signUp ifno
//        signupOUTPUT.write(signupINFO);          //Writes the info to the file
//    }
//
//    public PrintWriter createFile(String fileName) {
//        try {
//            File listOfNames = new File(fileName);   //Creates a File object
//            PrintWriter infoToWrite = new PrintWriter( //write chars to the console,file
//                    new BufferedWriter( //writes all info at one time
//                            new FileWriter(listOfNames)));
//            return infoToWrite;
//        } catch (IOException e) {
//            System.out.println("An I/O Error Occurred");
//            System.exit(0);
//        }
//        return null;
//
//    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        passwordLabel = new javax.swing.JLabel();
        jName = new javax.swing.JTextField();
        homeButton = new javax.swing.JButton();
        usernameLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        signUpButton = new javax.swing.JButton();
        lastNameLabel = new javax.swing.JLabel();
        phoneLabel = new javax.swing.JLabel();
        jPassword = new javax.swing.JTextField();
        jPhone = new javax.swing.JTextField();
        jRepass = new javax.swing.JTextField();
        repassLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        jEmail = new javax.swing.JTextField();
        jLastName = new javax.swing.JTextField();
        jUsername = new javax.swing.JTextField();
        messageLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Register");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SignUp", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 0, 36), new java.awt.Color(0, 0, 102))); // NOI18N

        passwordLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        passwordLabel.setText("Password");

        jName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jNameFocusGained(evt);
            }
        });

        homeButton.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        usernameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        usernameLabel.setText("Username");

        nameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        nameLabel.setText("Name");

        signUpButton.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        signUpButton.setText("SignUp");
        signUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signUpButtonActionPerformed(evt);
            }
        });

        lastNameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        lastNameLabel.setText("LastName");

        phoneLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        phoneLabel.setText("Phone");

        jPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordFocusGained(evt);
            }
        });

        jPhone.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPhoneFocusGained(evt);
            }
        });

        jRepass.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jRepassFocusGained(evt);
            }
        });

        repassLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        repassLabel.setText("Repass");

        emailLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        emailLabel.setText("Email");

        jEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jEmailFocusGained(evt);
            }
        });

        jLastName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jLastNameFocusGained(evt);
            }
        });

        jUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jUsernameFocusGained(evt);
            }
        });

        messageLabel.setForeground(new java.awt.Color(255, 0, 0));
        messageLabel.setText("YOU TYPED SOMETHING WRONG, TRY AGAIN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lastNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(phoneLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(emailLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(usernameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(passwordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(107, 107, 107)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                                    .addComponent(jRepass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                                    .addComponent(jUsername, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jEmail, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPhone, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLastName, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jName, javax.swing.GroupLayout.Alignment.LEADING)))
                            .addComponent(repassLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(255, 255, 255))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(messageLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(signUpButton)
                        .addGap(18, 18, 18)
                        .addComponent(homeButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(jName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameLabel)
                    .addComponent(jLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneLabel)
                    .addComponent(jPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLabel)
                    .addComponent(jEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(jUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(repassLabel)
                    .addComponent(jRepass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(homeButton)
                            .addComponent(signUpButton))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(messageLabel)
                        .addGap(28, 28, 28))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void signUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signUpButtonActionPerformed
        SignUp_Controller controller = new SignUp_Controller(new SignUp_Dao());
        SignUp_Validator validator = new SignUp_Validator();
        try {
            boolean signup = validator.validateSignUp(jName.getText(), jLastName.getText(), jPhone.getText(), jEmail.getText(), jUsername.getText(), jPassword.getText(), jRepass.getText());
            if (signup == true) {
                controller.controlSignUp(jName.getText(), jLastName.getText(), jPhone.getText(), jEmail.getText(), jUsername.getText(), jPassword.getText(), jRepass.getText());
                this.dispose();
                Login_Frame enabler = new Login_Frame();
                enabler.setVisible(true);
            } else {
                messageLabel.setVisible(true);

            }
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Error in SignUp_Frame");
        }
//        try {
//            boolean signup = controller.Validate(jName.getText(), jLastName.getText(), jPhone.getText(), jEmail.getText(), jUsername.getText(), jPassword.getText(), jRepass.getText());
//            if (signup == true) {
//                JOptionPane.showMessageDialog(null, "Success");
//                PrintWriter signupOUTPUT = createFile("C:\\Users\\Work\\Desktop\\customers.txt");
//                createSignUp(signupOUTPUT);
//                signupOUTPUT.close();
//            }
//        } catch (DaoException | HeadlessException | IllegalArgumentException e) {
//            JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.INFORMATION_MESSAGE);
//        }

    }//GEN-LAST:event_signUpButtonActionPerformed

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        this.dispose();
        MainFrame enabler = new MainFrame();
        enabler.setVisible(true);
    }//GEN-LAST:event_homeButtonActionPerformed

    private void jNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jNameFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jNameFocusGained

    private void jLastNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jLastNameFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jLastNameFocusGained

    private void jPhoneFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPhoneFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jPhoneFocusGained

    private void jEmailFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jEmailFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jEmailFocusGained

    private void jUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jUsernameFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jUsernameFocusGained

    private void jPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jPasswordFocusGained

    private void jRepassFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jRepassFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jRepassFocusGained

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SignUp_Frame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel emailLabel;
    private javax.swing.JButton homeButton;
    private javax.swing.JTextField jEmail;
    private javax.swing.JTextField jLastName;
    private javax.swing.JTextField jName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jPassword;
    private javax.swing.JTextField jPhone;
    private javax.swing.JTextField jRepass;
    private javax.swing.JTextField jUsername;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel phoneLabel;
    private javax.swing.JLabel repassLabel;
    private javax.swing.JButton signUpButton;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
}
